<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加分类' : '编辑分类'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
      center
    >
      <el-form :model="cateInfo" :rules="rules" ref="cateInfo">
        <el-form-item label="上级分类" :label-width="formLabelWidth" prop="pid">
          <el-select v-model="cateInfo.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in catelist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
          prop="catename"
        >
          <el-input v-model="cateInfo.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove='onRemove'
            :on-change="onChange"
            :on-upload = 'false'
            :file-list="fileList"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="cateInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="dialogInfo.isAdd" type="primary" @click="addCate"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="editMenu">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCate, postEditCate, getCateInfo } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      cateInfo: {
        pid: 0, //上级分类编号
        catename: "", //分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1, //状态1正常2禁用
      },
      formLabelWidth: "120px", //label宽度
      //规则验证
      rules: {
        catename: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级分类", trigger: "change" }],
      },
      //dialogVisible 预览图片是否显示
      dialogVisible:false,
      dialogImageUrl:'',
      fileList:[]
    };
  },
  computed: {
    ...mapGetters({
      catelist: "cate/getCateList",
    }),
  },
  props: ["dialogInfo"],
  methods: {
    //封装一个文件改变时触发的事件
    onChange(file,filelist){
      // console.log(file,'333333');
      // console.log(filelist,'44444');
      this.cateInfo.img = file.raw
    },
    //封装一个图片预览方法
    onPreview(file){
      // console.log(file,'文件');
      this.dialogImageUrl = file.url
      //让弹框显示
      this.dialogVisible = true
    },
    //封装一个图片删除
    onRemove(){

    },
    ...mapActions({
      getCateList: "cate/getCateListAction",
    }),
    //封装一个取消事件
    cancel() {
      //去改变父组件的isShow的状态
      this.$emit("cancel", false);
      //调取清空事件
      this.reset();
    },
    //封装一个清空事件
    reset() {
      this.cateInfo = {
        pid: 0, //上级分类编号
        catename: "", //分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1, //状态1正常2禁用
      };
      //调用清空验证的方法
      this.$refs.cateInfo.resetFields();
      //清空图片文件
      this.fileList = [];
    },
    // 确定事件
    addCate() {
      this.$refs.cateInfo.validate((valid) => {
        if (valid) {
          //验证成功调用添加接口
          // console.log(this.cateInfo,'表单对象');
          addCate(this.cateInfo).then((res) => {
            console.log(res, "添加分类");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel(); //关闭弹框
              this.getCateList(); //接口调用
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    //封装一个查询一条数据的接口
    lookup(id) {
      getCateInfo({ id }).then((res) => {
        if (res.code == 200) {
          this.cateInfo = res.list;
          this.cateInfo.id = id;
          //图片数据回显
          this.fileList = [{url:this.$http+this.cateInfo.img}]
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 编辑事件
    editMenu() {
      this.$refs.cateInfo.validate((valid) => {
        if (valid) {
          postEditCate(this.cateInfo).then((res) => {
            console.log(res, "添加分类");
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel(); //关闭弹框
              this.getCateList(); //接口调用
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
  },
};
</script>
<style scoped></style>
